﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class Student_Exam : StudentPageBase {
    protected int id = 0;
    protected Exam.Core.Model.ExamPaper model = new Exam.Core.Model.ExamPaper();
    protected int qnumber=0;
    protected int SEId = 0;
    protected StringBuilder sbtype = new StringBuilder();//保存题型
    protected StringBuilder sb = new StringBuilder();//保存题型和题目编号
    protected string paperfilepath = string.Empty;//试卷路径
    protected void Page_Load(object sender, EventArgs e) {
        if (Request.QueryString["id"] != null && Request.QueryString["id"] != "") {
            if (!int.TryParse(Request.QueryString["id"].Trim(), out id)) {
                Response.Write("发生错误!");
                Response.End();
            }
        }
        if (!IsPostBack) {

            getPaper();//获取试卷

            if (model.AllowRe.Trim() == "0") {//不允许重考

                //判断考生是否已经考过并交卷了
                string SQL = "SELECT PaperStatus FROM StudentPaper WHERE StudentID=" + isTheMe.StudentID.Trim() + " AND PaperID=" + id;
                object obj = Exam.Core.DAL.DataProvider.GetSingle(SQL);
                if (null != obj) {
                    if (obj.ToString().Trim() == "2") {
                        Response.Redirect("finesh.aspx", true);//状态为2 表示已经考完
                    }
                }
                //
                SQL = "SELECT ExamStatus FROM StudentExam WHERE StudentID=" + isTheMe.StudentID.Trim() + " AND PaperID=" + id;
                obj = Exam.Core.DAL.DataProvider.GetSingle(SQL);
                if (null != obj) {
                    if (obj.ToString().Trim() == "2") {
                        Response.Redirect("finesh.aspx", true);//状态为2 表示已经考完
                    }
                }

            }
            


            

            paperfilepath = model.PaperContent.Trim();

            for (int t = 0; t < model.ExamQuestionTypeList.Count; t++) {

                sbtype.Append(model.ExamQuestionTypeList[t].QuestionTypeId);
                sbtype.Append("|");

                System.Collections.Generic.List<Exam.Core.Model.ExamQuestion> objQs = model.ExamQuestionTypeList[t].QuestionList;
                for (int l = 0; l < objQs.Count; l++) {
                    sb.Append(model.ExamQuestionTypeList[t].QuestionTypeId + "_" + objQs[l].QuestionId + ",");
                }
                if (sb.Length > 0)
                {
                    sb.Length--;
                }
                sb.Append("|");
            }

            //将考生ID 与 试卷ID 开始时间 状态 写入考生考卷表
            SEId = new Exam.Core.DAL.StudentExam().StuBeginExam(id, isTheMe.StudentID, DateTime.Now.ToString(), 1);

        }
    }
    private void getPaper() {
        model = new Exam.Core.DAL.Paper().getPaperById(id);
    }

    protected string getNumber(int number) {
        qnumber = qnumber + 1;//计算题目总数
        string result = number.ToString();
        StringBuilder sb = new StringBuilder();
        foreach (char c in result) {
            //<img src='../images/number/<%=getNumber(k+1) %>r.gif' border=0 />
            sb.Append("<img src='../images/number/" + c + "r.gif' border=0 />");
        }
        return sb.ToString();
    }
}
